package com.gzk.system;

import com.gzk.basic.query.BaseQuery;
import com.gzk.system.domain.LoginUser;
import com.gzk.system.domain.WebLog;
import com.gzk.system.mapper.WebLogMapper;
import com.gzk.system.service.IWebLogService;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

//@Aspect
//@Component
public class LogManager {

    @Autowired
    private WebLogMapper webLogMapper;

    @Pointcut("execution(* com.gzk.org.service.I*Service.*(..))")
    public void orgPointCut() {
    }

    @Pointcut("execution(* com.gzk.basic.service.IBaseService.*(..))")
    public void basePointCut() {
    }

    @Around("orgPointCut()||basePointCut()")
    public Object around(ProceedingJoinPoint joinPoint) {
        try {
            //这个RequestContextHolder是Springmvc提供来获得请求的东西
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();

            String ip = request.getRemoteAddr();//获取ip
            LoginUser user = (LoginUser)request.getSession().getAttribute("user");
            String username = "";
            if(user==null){
                username = "GZK";
            }else {
                username = user.getName();
            }

            String method = joinPoint.getSignature().getDeclaringTypeName() + "."+joinPoint.getSignature().getName();//执行的方法  List com.gzk.org.service.impl.DepartmentServiceImpl.getTree()
            //防止service层返回值为空
            Object proceed = joinPoint.proceed();


            //获取方法上面的描述
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            MyLog annotation = signature.getMethod().getAnnotation(MyLog.class);
            String description = "";
            if(annotation!=null){
                System.out.println();
                description = annotation.description();
            }

            //
            WebLog log = new WebLog();
            log.setMethod(method);
            log.setIp(ip);
            log.setUsername(username);
            log.setDescription(description);

            webLogMapper.save(log);

            return proceed;
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {

        }
        return null;
    }
}
